<?php
class cus extends CI_Controller {
	function index() {
		$this -> pub -> logp('浏览客服', '2-3');
		$da = $this -> pub -> get_setting('客服', 'Customer service');
		$this -> load -> view('adminx/cus', $da);
	}

	function get() {
		$this -> pub -> logp('获取客服信息', '2-3');
		if ($this -> session -> userdata('state') == 2) {
			$sid = $this -> input -> post('idx') ? $this -> input -> post('idx') : $this -> session -> userdata('sid');
		} else {
			$sid = $this -> session -> userdata('sid');
		}
		$this -> db -> where('staff_id', $sid);

		if ($this -> input -> post('key')) {
			$this -> db -> select('count(*)');
			$this -> db -> like('nickname', $this -> input -> post('key'));
			if ($this -> input -> post('type') > 0) {
				$this -> db -> where('type', $this -> input -> post('type'));
			}
			$q = $this -> db -> get_where('member');
			$ls = $q -> row_array(0);
			$num = $ls['count(*)'];
			$this -> db -> like('nickname', $this -> input -> post('key'));
		} else {
			if ($this -> input -> post('type') > 0) {
				$this -> db -> where('type', $this -> input -> post('type'));
			}
			$this -> db -> select('count(*)');
			$q = $this -> db -> get_where('member');
			$ls = $q -> row_array(0);
			$num = $ls['count(*)'];
		}
		$this -> db -> where('staff_id', $sid);
		//一共的页数
		$da['num'] = ceil($num / 30);

		$page = is_numeric($this -> input -> post('page')) && $this -> input -> post('page') > 0 ? $this -> input -> post('page') : 1;
		$page = $page > $da['num'] ? $da['num'] : $page;
		$da['page'] = $page;

		$page = ($page - 1) == -1 ? 0 : ($page - 1);

		$this -> db -> limit(30, $page * 30);

		$this -> db -> order_by('mid', 'desc');
		if ($this -> input -> post('type') > 0) {
			$this -> db -> where('type', $this -> input -> post('type'));
		}
		$q = $this -> db -> get_where('member');
		$da['content'] = '';
		foreach ($q->result_array() as $v) {
			$da['content'] .= '<tr>';
			$da['content'] .= '<td><a target="_blank" href="/iadmin/member/info/' . $v['mid'] . '"><img width="48" src="' . ($v['headimgurl'] ? $v['headimgurl'] : '/style/images/tx.png') . '" /></a></td>';
			$da['content'] .= '<td>' . $v['nickname'] . '</td>';
			$da['content'] .= '<td>' . $v['name'] . '</td>';
			$da['content'] .= '<td>' . $v['phone'] . '</td>';
			$da['content'] .= '<td>' . ($v['sex'] == 1 ? '男' : '女') . '</td>';
			$da['content'] .= '<td>' . ($v['subscribe'] == 1 ? '订阅' : '未订阅') . '</td>';
			$da['content'] .= '<td>' . date('Y-m-d', $v['subscribe_time']) . '</td>';
			$da['content'] .= '<td><button onclick="del(\'' . $v['mid'] . '\',\''.$v['nickname'].'\')" type="button" class="btn red"><i class="fa fa-trash-o"></i> 删除</button></td>';
			$da['content'] .= '</tr>';
		}
		$da['type'] = 1;
		echo json_encode($da);
	}

	function del() {
		$this -> pub -> logp('删除客服信息', '2-3');
		$this -> db -> where('mid', $this -> input -> post('idx'));
		$this -> db -> update('member', array('state' => 1, 'staff_id' => ''));
		echo json_encode(array('type' => 1));
	}

}
?>